﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility1;

namespace eDream
{
    public partial class Canhan : System.Web.UI.Page
    {
 
        private string pageName;
        public string pathUrl = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //RouteTable.Routes.Ignore("");
            RouteTable.Routes.Clear();
            pathUrl = WebUtils.pathUrl();
            UserDB user_obj = (UserDB)Session[Global.SESS_USER];
            pageName = Utils.GetParameter("page", "home");
            if (!IsPostBack)
            {
                try
                {
                    if (user_obj.q == 99)
                    {
                        ASPxNavBar1.Groups.FindByName("nAdmin").Visible=true;
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("acti").Visible = true;
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("kiem_tra_len_cap").Visible = true;
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Dang_ky").Visible = true;
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Nap_tien").Visible = true;
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Quan_ly_san_pham").Visible = true;
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("them_san_pham").Visible = true;
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("nhom_san_pham").Visible = true;
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Cau_hinh").Visible = true;
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Address").Visible = true;
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Don_hang").Visible = true;

                        hdfPage1.Value = "1";
                        hdfPage2.Value = "2";
                        hdfPage3.Value = "3";
                        hdfPage4.Value = "4";
                        hdfPage5.Value = "5";
                        hdfPage6.Value = "6";
                        hdfPage7.Value = "7";
                        hdfPage8.Value = "8";
                        hdfPage9.Value = "9";
                        hdfPage10.Value = "10";
                        hdfPage11.Value = "11";
                        hdfPage12.Value = "12";
                        hdfPage13.Value = "13";
                        hdfPage14.Value = "14";
                    }
                    else if (user_obj.q > 1 && user_obj.q < 99)
                    {
                        ASPxNavBar1.Groups.FindByName("nAdmin").Visible = true;
                        Kiemtraquyen(user_obj.ID);
                    }
                    else if (user_obj.q == 0)
                    {
                        ASPxNavBar1.Groups.FindByName("nAdmin").Visible = false;
                        ASPxNavBar1.Groups.FindByName("hoahong").Visible = false;
                      
                    }
                }
                catch (Exception) { }
            }
            if (user_obj != null)
            {
                if (user_obj.kichhoat == true)
                {
                    //if (RouteData.Values["namec"] != null)
                       // pageName = RouteData.Values["namec"].ToString();
                    //if (RouteData.Values["idc"] != null)
                        //id = RouteData.Values["idc"].ToString();
                   
                   

                    getPageName(pageName);
                }
                else
                {
                    string link = pathUrl + "/Trang-chu.html";
                    Response.Redirect(link);
                }
            }
            else
            {
                string link = pathUrl + "/Trang-chu.html";
                Response.Redirect(link);
            }
        }

        

        #region Common
        private void getPageName(string pageName)
        {
            try
            {

                switch (pageName)
                {

                    case "Doanh-so-ban-the":
                        pageName = SiteNavigation.contronl_Chitiethoahong;
                        hdfTitle.Value = "Doanh số bán thẻ";
                        break;
                    case "So-do-he-thong":
                        pageName = SiteNavigation.contronl_Sodocay;
                        hdfTitle.Value = "Quản lý nhóm";
                        break;
                    case "so-do":
                        pageName = SiteNavigation.contronl_Sodocay;
                        hdfTitle.Value = "Quản lý nhóm";
                        break;
                    
                   
                    case "Thong-ke-hoa-hong":
                        pageName = SiteNavigation.contronl_hhTuvan_tructiep;
                        hdfTitle.Value = "Thống kê Chi tiết";
                        break;
                    case "Thay-mat-khau":
                        pageName = SiteNavigation.contronl_changeMK;
                        hdfTitle.Value = "Thay đổi mật khẩu";
                        break;
                    case "Doanh-so-nhom":
                        pageName = SiteNavigation.contronl_Doanhsonhom;
                        hdfTitle.Value = "Doanh số nhóm";
                        break;
                    case "Ho-so-ca-nhan":
                        pageName = SiteNavigation.contronl_IntroduceMysef;
                        hdfTitle.Value = "Hồ sơ cá nhân";
                        break;
                    case "Hoa-hong-ban-le":
                        pageName = SiteNavigation.contronl_doanhso_banhang;
                        hdfTitle.Value = "Hoa hồng bán lẻ";
                        break;
                                      
                   
                 //-------admin--------------------------------------------
                    case "Kich-hoat-tai-khoan":
                        if (hdfPage1.Value == "1")
                            pageName = SiteNavigation.contronl_Acti;
                        hdfTitle.Value = "Kích hoạt tài khoản";
                        break;
                    case "kiem-tra-len-cap":
                        if (hdfPage2.Value == "2")
                        pageName = SiteNavigation.contronl_kiem_tra_len_cap;
                        hdfTitle.Value = "Kiểm tra lên cấp và tính lương";
                        break;
                    case "Nap-tien":
                        if (hdfPage3.Value == "3")
                        pageName = SiteNavigation.contronl_nap_tien;
                        hdfTitle.Value = "Quản lý nạp tiền";
                        break;
                    case "Dang-ky":
                        if (hdfPage4.Value == "4")
                            pageName = SiteNavigation.contronl_Dangky;
                        hdfTitle.Value = "Đăng ký tài khoản";
                        break;
                    case "Quan-ly-san-pham":
                        if (hdfPage5.Value == "5")
                        pageName = SiteNavigation.contronl_Quanly_sanpham;
                        hdfTitle.Value = "Quản lý sản phẩm";
                        break;
                    case "them-san-pham":
                        if (hdfPage6.Value == "6")
                        pageName = SiteNavigation.contronl_Them_san_pham;
                        hdfTitle.Value = "Quản lý sản phẩm";
                        break;
                    case "nhom-san-pham":
                        if (hdfPage7.Value == "7")
                        pageName = SiteNavigation.contronl_nhom_san_pham;
                        hdfTitle.Value = "Quản lý nhóm sản phẩm";
                        break;
                    case "Cau-hinh":
                        if (hdfPage8.Value == "8")
                        pageName = SiteNavigation.contronl_cau_hinh;
                        hdfTitle.Value = "Cấu hình hệ thống";
                        break;
                    case "Address":
                        if (hdfPage9.Value == "9")
                            pageName = SiteNavigation.contronl_address;
                        hdfTitle.Value = "Cấu hình hệ thống";
                        break;
                    case "Lich-su-nap-tien":
                        if (hdfPage10.Value == "10")
                        pageName = SiteNavigation.contronl_Lich_su_nap_tien;
                        hdfTitle.Value = "Quản lý nạp tiền";
                        break;
                    case "Don-hang":
                        if (hdfPage11.Value == "11")
                            pageName = SiteNavigation.contronl_quanly_donhang;
                        hdfTitle.Value = "Quản lý đơn hàng";
                        break;
                    case "Chi-tiet-don-hang":
                        if (hdfPage12.Value == "12")
                            pageName = SiteNavigation.contronl_quanly_Chitietdonhang;
                        hdfTitle.Value = "Quản lý đơn hàng";
                        break;
                    case "Tai-lieu":
                        //if (hdfPage12.Value == "12")
                        pageName = SiteNavigation.contronl_quanly_Documnet;
                        hdfTitle.Value = "Tài liệu Msmart";
                        break;
                    case "Feback":
                        if (hdfPage13.Value == "13")
                        pageName = SiteNavigation.contronl_quanly_goiy;
                        hdfTitle.Value = "Góp ý Msmart";
                        break;
                    case "Feback-detail":
                        if (hdfPage14.Value == "14")
                        pageName = SiteNavigation.contronl_quanly_goiy_detail;
                        hdfTitle.Value = "Góp ý Msmart";
                        break;
                    case "san-pham-da-mua":
                         pageName = SiteNavigation.contronl_Donhanglog;
                        hdfTitle.Value = "Sản phẩm đã mua";
                        break;
                    case "Tin-tuc-edit":
                        pageName = SiteNavigation.contronl_Edit_New;
                        hdfTitle.Value = "Quản lý tin tức";
                        break;
                        
                    case "Quan-ly-User":
                        hdfTitle.Value = "Quản lý User";
                        pageName = SiteNavigation.contronl_User;
                        break;
                    case "wTintuc-Slider":
                        pageName = SiteNavigation.contronl_quanly_wTintuc_slider;
                        hdfTitle.Value = "Quản lý tin tức";
                        break;
                    case "wTintuc-New":
                        pageName = SiteNavigation.contronl_quanly_wTintuc_New;
                        hdfTitle.Value = "Quản lý tin tức";
                        break;
                }

                UserControl contentView = (UserControl)Page.LoadControl(pageName);
                phdContent.Controls.Add(contentView);
            }
            catch (Exception ex)
            {
                // Response.Redirect(SiteNavigation.LoginPageDMS);
            }
        }
        #endregion

        void Kiemtraquyen(int id)
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
           

            try
            {

                dx.Connection.Open();
                var list = from k in dx.Power_tbs
                           where k.RoleID == id
                           select k;
                foreach (Power_tb p in list)
                {
                   // Session["quyen"] = p.AcView.ToString();
                    switch(p.IDPages)
                    {
                        case 1:
                            ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("acti").Visible = true;
                            hdfPage1.Value = "1";
                        break;
                        case 2:
                            ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("kiem_tra_len_cap").Visible = true;
                            hdfPage2.Value = "2";
                        break;
                        case 3:
                            ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Nap_tien").Visible = true;
                            hdfPage3.Value = "3";
                        break;
                        case 4:
                            ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Dang_ky").Visible = true;
                            hdfPage4.Value = "4";
                        break;
                        case 5:
                            ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Quan_ly_san_pham").Visible = true;
                            hdfPage5.Value = "5";
                        break;
                        case 6:
                            ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("them_san_pham").Visible = true;
                            hdfPage6.Value = "6";
                        break;
                        case 7:
                            ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("nhom_san_pham").Visible = true;
                            hdfPage7.Value = "7";
                        break;
                        case 8:
                            ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Cau_hinh").Visible = true;
                            hdfPage8.Value = "8";
                        break;
                        case 9:
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Address").Visible = true;
                            hdfPage9.Value = "9";
                        break;
                        case 10:                   
                            hdfPage10.Value = "10";
                        break;
                        case 11:
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Don_hang").Visible = true;
                        hdfPage11.Value = "11";
                        break;
                        case 12:
                        //ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Don_hang").Visible = true;
                        hdfPage12.Value = "12";
                        break;
                        case 13:
                        ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Feback").Visible = true;
                        hdfPage13.Value = "13";
                        break;
                        case 14:
                        //ASPxNavBar1.Groups.FindByName("nAdmin").NavBar.Items.FindByName("Don_hang").Visible = true;
                        hdfPage14.Value = "14";
                        break;
                    }
                }

            }
            catch (Exception) { }
            finally { dx.Connection.Close(); }
        }
    }
}